package org.example.option;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.junit.jupiter.api.Test;

import java.util.Optional;
import java.util.function.Function;

/**
 * file:OptionsTest.class
 * function:
 *
 * @author shaodan wang
 * @date 2024-10-02
 */

public class OptionsTest {

    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    public static class MyUser {
        private String name;
        private int age;
        private String[] hobby;
    }


    @Test
    public void createOptions() {

        MyUser myUser = new MyUser();
        MyUser myUser1 = new MyUser("kunkun", 18, new String[]{"唱", "跳", "rap"});
        MyUser myUser2 = new MyUser("tom", 12, new String[]{"篮球", "跳", "rap"});
        Optional<MyUser> optional = Optional.ofNullable(myUser1);//传入的对象不能为空，为空则报错


      Optional<MyUser> o1 =   optional.flatMap(new Function<MyUser, Optional<MyUser>>() {
          @Override
          public Optional<MyUser> apply(MyUser aa) {
              return Optional.of(new MyUser());
          }
      });

        System.out.println(o1);

       Optional<String> o2 =  optional.map(new Function<MyUser, String>() {
            @Override
            public String apply(MyUser myUser) {
                return "aaaaa";
            }
        });

    }

}
